package com.ideal.controller;

import com.github.pagehelper.util.StringUtil;
import com.ideal.dto.ResultDto;
import com.ideal.dto.user.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.ResultSet;
import java.util.*;

/**
 * @Author songfz
 * @Description 用户组业务
 * @Date 2022/7/25
 */
@Controller
@RequestMapping("api/role")
public class UserGroupController {

    private static List<String> groups = new ArrayList<>();

    private static  List<UserDto> userDtos = new ArrayList<>();

    private static  Map<String,List<String>> userGroup = new HashMap<>();
    static {

        groups.add("F功能组A");
        groups.add("F功能组B");
        groups.add("F功能组C");
        groups.add("F功能组D");
        groups.add("F功能组E");


        UserDto userDto1 =  UserDto.builder().account("100001").userName("李四").build();
        UserDto userDto2 =  UserDto.builder().account("100002").userName("王五").build();
        UserDto userDto3 =  UserDto.builder().account("100003").userName("赵一").build();
        UserDto userDto4 =  UserDto.builder().account("100004").userName("马二").build();
        UserDto userDto5 =  UserDto.builder().account("100005").userName("张三").build();
        userDtos.add(userDto1);
        userDtos.add(userDto2);
        userDtos.add(userDto3);
        userDtos.add(userDto4);
        userDtos.add(userDto5);
    }


    @RequestMapping(value = "user/info",method = RequestMethod.GET)
    @ResponseBody
    public ResultDto userInfo(){
        final UserDto userDto = userDtos.get(0);
         List<String> groupList = new ArrayList<>();
        for (String group : userGroup.keySet()) {
            final List<String> accountList = userGroup.get(group);
            if (accountList.contains(userDto.getAccount())) {
                groupList.add(group);
            }
        }
        userDto.setGroup(groupList);
        return ResultDto.success(userDto);
    }
    /**
     * 功能组列表
     * @return
     */
    @RequestMapping(value = "/group/list",method = RequestMethod.GET)
    @ResponseBody
    private ResultDto groupList(){

        return ResultDto.success(groups);
    }

    /**
     * 用户列表
     * @return
     */
    @RequestMapping(value = "/user/list",method = RequestMethod.GET)
    @ResponseBody
    public ResultDto userList(String userName,String group){

        if (!StringUtil.isEmpty(userName)) {
            List<UserDto> newUserList = new ArrayList<>();
            for (UserDto userDto : userDtos) {
                if (userDto.getUserName().equals(userName)) {
                    newUserList.add(userDto);
                }
            }
           return ResultDto.success(newUserList);
        }

        if (!StringUtil.isEmpty(group)) {
            final List<String> userList = userGroup.get(group);
            for (UserDto userDto : userDtos) {
                if (!CollectionUtils.isEmpty(userList) && userList.contains(userDto.getAccount())) {
                        userDto.setIsSelect(true);
                }else {
                    userDto.setIsSelect(false);
                }

            }
            Collections.sort(userDtos, new Comparator<UserDto>() {
                @Override
                public int compare(UserDto o1, UserDto o2) {
                    return (o1.getIsSelect()==true?0:1) - (o2.getIsSelect()==true?0:1);
                }
            });
        }
        return ResultDto.success(userDtos);
    }

    /**
     * 功能组添加用户
     * @param userGroupParam
     * @return
     */
    @RequestMapping(value = "group/user/add",method = RequestMethod.PUT)
    @ResponseBody
    public ResultDto addUserToGroup(@RequestBody UserGroupParam userGroupParam){

        final List<String> userList = userGroup.get(userGroupParam.getGroup());
        if (!CollectionUtils.isEmpty(userList)) {

            userList.clear();
        }

            userGroup.put(userGroupParam.getGroup(),userGroupParam.getAccount());
        //for (UserDto userDto : userDtos) {
        //    if (userDto.getAccount().equals(account)) {
        //        List<String> userGroup = userDto.getGroup();
        //        if (CollectionUtils.isEmpty(userGroup)) {
        //            userGroup = new ArrayList<>();
        //        }
        //        userGroup.add(group);
        //        userDto.setIsSelect(true);
        //        userDto.setGroup(userGroup);
        //        break;
        //    }
        //}

       return ResultDto.success(null);

    }
}
 class UserGroupParam {

    private String group;
    private List<String> account;

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public List<String> getAccount() {
        return account;
    }

    public void setAccount(List<String> account) {
        this.account = account;
    }
}
